/* main.c --- 
 * 
 * Filename: main.c
 * Description: Application entry point for STM32F103-STK demo 
 * Author: Molnár Zoltán
 * Created: Thu Oct 24 13:46:36 2013 (+0200)
 * Version: 
 * Last-Updated: Thu Oct 24 14:32:22 2013 (+0200)
 *           By: Molnár Zoltán
 *     Update #: 6
 * 
 */

/* This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth
 * Floor, Boston, MA 02110-1301, USA.
 */

/*******************************************************************************/
/* INCLUDES                                                                    */
/*******************************************************************************/
/* Application-specific headers */
#include <stm32f10x.h>
#include <lcd.h>
#include <stm32f103-stk.h>
/* Include FreeRTOS-related headers */
#include <FreeRTOS.h>
#include <task.h>

/*******************************************************************************/
/* DEFINED CONSTANTS                                                           */
/*******************************************************************************/
#define mainDELAY_LOOP_COUNT 7200000

/*******************************************************************************/
/* MACRO DEFINITIONS                                                           */
/*******************************************************************************/


/*******************************************************************************/
/* TYPE DEFINITIONS                                                            */
/*******************************************************************************/


/*******************************************************************************/
/* DEFINITIONS OF GLOBAL CONSTANTS AND VARIABLES                               */
/*******************************************************************************/


/*******************************************************************************/
/* DEFINITION OF LOCAL FUNCTIONS                                               */
/*******************************************************************************/
/**
 * Setup hardware
 */
static void prvSetupHardware( void )
{
  
  /* Initialize processor core */
  SystemInit();

  /* Initialize LED */
  vBoardInitLed();
  
}

/*******************************************************************************/
/* DEFINITION OF GLOBAL FUNCTIONS                                              */
/*******************************************************************************/
/**
 * Task1 : Blink LED 
 **/
void vTask1 (void *pvParameters)
{
  /* As per most tasks, this task is implemented in an infinite loop. */
  for( ;; )
    {
      GPIO_SetBits(LEDPORT, LEDPIN);   
      vTaskDelay( 500 / portTICK_RATE_MS);

      GPIO_ResetBits(LEDPORT, LEDPIN);   
      vTaskDelay( 500 / portTICK_RATE_MS);
    }
}

/**
 * Task2 : do nothing 
 */
void vTask2 (void *pvParameters)
{
  for( ;; )
    {
      vTaskDelay( 1000 / portTICK_RATE_MS);
    }
}

/**
 * Stack-overflow hook function
 */
void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )
{
  /* This function will get called if a task overflows its stack.   If the
     parameters are corrupt then inspect pxCurrentTCB to find which was the
     offending task. */
  
  ( void ) pxTask;
  ( void ) pcTaskName;
  
  for( ;; );
}

/**
 * Main function, application entry point 
 */
int main (void)
{
  uint32_t cntr;
  
  /* Setup the hardware */
  prvSetupHardware();

  /* LCD init */
  LCDInit();
  LCDContrast(0x45);

  LCDClear();
  LCDStr ( 0, "STM32F103-STK", 0 );
  LCDUpdate();

  for(cntr = 0; cntr < mainDELAY_LOOP_COUNT; cntr++);

  LCDClear();
  LCDStr ( 4, "START", 0 );
  LCDUpdate();

  /* Create one of the two tasks. Note that a real application should check
     the return value of the xTaskCreate() call to ensure the task was created
     successfully. */
  xTaskCreate( vTask1, "Task 1", 1000, NULL, 1, NULL ); 

  /* Create the other task in exactly the same way and at the same priority. */
  //  xTaskCreate( vTask2, "Task 2", 1000, NULL, 1, NULL );
  xTaskCreate( vTask2, "Task 2", 1000, NULL, 1, NULL ); 

  /* Start the scheduler so the tasks start executing. */
  vTaskStartScheduler();

  /* If all is well then main() will never reach here as the scheduler will
     now be running the tasks. If main() does reach here then it is likely that
     there was insufficient heap memory available for the idle task to be created.
     CHAPTER 5 provides more information on memory management. */
  for( ;; );
  
  return 0;

}


/******************************* END OF FILE ***********************************/

